package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.CustInfo;
import com.ruoyi.system.service.ICustInfoService;
import com.ruoyi.system.vo.CustInfoVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户登记Controller
 *
 * @author ruoyi
 * @date 2020-02-12
 */
@Controller
@RequestMapping("/system/cust")
public class CustInfoController extends BaseController {
    private String prefix = "system/cust";

    @Autowired
    private ICustInfoService custInfoService;

    @RequiresPermissions("system:cust:view")
    @GetMapping()
    public String info() {
        return prefix + "/info";
    }

    /**
     * 查询客户登记列表
     */
    @RequiresPermissions("system:cust:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CustInfo custInfo) {
        startPage();
        List<CustInfo> list = custInfoService.selectCustInfoList(custInfo);
        return getDataTable(list);
    }

    /**
     * 导出客户登记列表
     */
    @RequiresPermissions("system:cust:export")
    @Log(title = "客户登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CustInfo custInfo) {
        List<CustInfo> list = custInfoService.selectCustInfoList(custInfo);
        List<CustInfoVO> custInfoVOS = BeanUtils.listCopy(list, CustInfoVO.class);
        ExcelUtil<CustInfoVO> util = new ExcelUtil<CustInfoVO>(CustInfoVO.class);
        return util.exportExcel(custInfoVOS, "客户登记");
    }

    /**
     * 新增客户登记
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存客户登记
     */
    @RequiresPermissions("system:cust:add")
    @Log(title = "客户登记", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CustInfo custInfo) {
        custInfo.setCreateUserId(ShiroUtils.getUserId());
        return toAjax(custInfoService.insertCustInfo(custInfo));
    }

    /**
     * 修改客户登记
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        CustInfo custInfo = custInfoService.selectCustInfoById(id);
        mmap.put("custInfo", custInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存客户登记
     */
    @RequiresPermissions("system:cust:edit")
    @Log(title = "客户登记", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CustInfo custInfo) {
        custInfo.setUpdateUserId(ShiroUtils.getUserId());
        return toAjax(custInfoService.updateCustInfo(custInfo));
    }

    /**
     * 删除客户登记
     */
    @RequiresPermissions("system:cust:remove")
    @Log(title = "客户登记", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(custInfoService.deleteCustInfoByIds(ids));
    }
}
